package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.core.utils.EncryptHelper;
import com.fzrskj.cloud.free.core.utils.EncryptUtils;
import com.fzrskj.cloud.free.sys.bean.Param;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;
import ms.core.tool.StrTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CParamDao extends CObject {
	//private declaration
	IDbDrv db = null;

	//constructor function to initialize database
	public CParamDao(){
		db = CloudDbDrvUtils.getDbUtilsDrv();
	}

	//dispose function implements to free resource
	@Override
	public void dispose(){
		super.dispose();
		db.closeConnection();
		db = null;
	}

	//to update parameter value & remark & show index
	public int modParam(String pName, String pValue){
		String sql = "update dic_param set paramValue=? where paramName=? and ((agentId=? and (paramType=1 or paramType=3)) or paramType=2)";
		Object[] params = {pValue,pName, CloudContextUtils.getAgentId()};
		int ret = db.executeSql(sql, params);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(err);
		return ret;
	}

	/**get parameter by parameter name
	 * @param pName
	 * @return
	 */
	public Param queryParam(String pName){
		String strSql = "select * from dic_param where paramName=? and ((agentId=? and (paramType=1 or paramType=3)) or paramType=2)";
		Object[] params = {pName, CloudContextUtils.getAgentId()};
		Param ret = db.queryForBean(strSql, Param.class, params);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		setErrCode(err);
		return ret;
	}

	/**to query parameter by pType and pCode
	 * update by lms 2016.09.21
	 * @param pType if pType=-1,means ignore this parameter 
	 * @param pName if pName is null, ignore this parameter
	 * @return
	 */
	public List<Param> queryParam(int pType, String pName){
		Map<String, Object> cdt = new HashMap<>();
		//代理商数据隔离
		cdt.put("agentId", CloudContextUtils.getAgentId());

		//特殊字段，取主站配置，张杰，2019-12-18
		List<String> specialKey = new ArrayList<String>(){{
			this.add("cloudPromotion");
		}};
		if (specialKey.contains(pName)) {
			cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);
		}

		if (!StrTool.isBlankStr(pName)) {
			cdt.put("paramName", pName);
		}
		if (pType>=0) {
			cdt.put("paramType", pType);
		}
		List<Param> ret = db.queryForBeans("dic_param", "*", " id desc ", Param.class, cdt, -1, -1);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		setErrCode(err);
		if (null != ret) {
			ret.forEach(item->{
				if (!StrTool.isBlankStr(item.getParamName()) && EncryptHelper.encryptSysParam.contains(item.getParamName()))  {
					item.setParamValue(EncryptUtils.decodeSave(item.getParamValue()));
				}
			});
		}
		return ret;
	}
}
